﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTTB.Assembler;
using DTTB.DAO;
using DTTB.Domain;
using DTTB.DTO;

namespace DTTB.SERVICE
{
    public class UsersService
    {
        UsersAssembler UsersAssembler = new UsersAssembler();
        UsersInRolesAssembler UsersInRolesAssembler = new UsersInRolesAssembler();
        UsersDao UsersDao  = new UsersDao();
        UsersInRolesDao UsersInRolesDao = new UsersInRolesDao();
        PermissionsPerUsersDao PermissionsPerUsersDao = new PermissionsPerUsersDao();
        PermissionsPerUsersAssembler PermissionsPerUsersAssembler = new PermissionsPerUsersAssembler();

        public IList<UsersDto> GetAllUsers()
        {
            IList<Users> usersList = UsersDao.GetAllUsers();

            IList<UsersDto> usersDtoList = UsersAssembler.ConvertToDataTransferObjectList(usersList);

            return usersDtoList;
        }

        public IList<UsersDto> GetAllRolesIsEnable()
        {
            IList<Users> usersList = UsersDao.GetAllUsers();

            IList<UsersDto> usersDtoList = UsersAssembler.ConvertToDataTransferObjectList(usersList);

            return usersDtoList;
        }

        public IList<UsersDto> GetAllUsersActive()
        {
            IList<Users> usersList = UsersDao.GetAllUsersActive();

            IList<UsersDto> usersDtoList = UsersAssembler.ConvertToDataTransferObjectList(usersList);

            return usersDtoList;
        }

        /// <summary>
        /// Lấy tất cả user trong một phòng ban xác định
        /// </summary>
        /// <returns></returns>
        public IList<UsersDto> GetAllUsersActiveInDepartment(string departmentId)
        {
            IList<Users> usersList = UsersDao.GetAllUsersActiveInDepartment(departmentId);

            IList<UsersDto> usersDtoList = UsersAssembler.ConvertToDataTransferObjectList(usersList);

            return usersDtoList;
        }

        public UsersDto GetUsersById(string Id)
        {
            Users users = UsersDao.GetUsersById(Id);

            UsersDto usersDto = UsersAssembler.ConvertToDataTransferObject(users);

            return usersDto;
        }

        public UsersDto GetIsActiveUsersById(string Id)
        {
            Users users = UsersDao.GetIsActiveUsersById(Id);

            UsersDto usersDto = UsersAssembler.ConvertToDataTransferObject(users);

            return usersDto;
        }

        public UsersDto GetUsersByUserName(string UserName)
        {
            Users users = UsersDao.GetUsersByUserName(UserName);

            UsersDto usersDto = UsersAssembler.ConvertToDataTransferObject(users);

            return usersDto;
        }

        public UsersDto GetUsersByUserNamePassword(string UserName , string password)
        {
            Users users = UsersDao.GetUsersByUserNamePassword(UserName, password);

            UsersDto usersDto = UsersAssembler.ConvertToDataTransferObject(users);

            return usersDto;
        }
        public bool AddNewUsers(UsersDto usersDto)
        {
            return UsersDao.AddNewUsers(UsersAssembler.ConvertToBusinessObject(usersDto));
        } 
        
        public bool AddNewUsers(UsersDto usersDto,ref int idReturn )
        {
            return UsersDao.AddNewUsers(UsersAssembler.ConvertToBusinessObject(usersDto), ref idReturn);
        }

        public bool Update(UsersDto usersDto)
        {
            return UsersDao.Update(UsersAssembler.ConvertToBusinessObject(usersDto));
        }

      public bool Delete(string userID)
      {
        return UsersDao.Delete(userID);
      }

      //[Transaction] : cần thiết lập
      public void UpdateRolesPerUsers(IList<UsersInRolesDto> usersInRolesDtos, string usersID)
      {
          ////Delete all RolesInUsers of existing User
          this.UsersInRolesDao.DeleteAllUserInRolesByUserId(usersID);

          //update new setting
          foreach (var dto in usersInRolesDtos)
          {
              this.UsersInRolesDao.AddNewUsersInRoles(this.UsersInRolesAssembler.ConvertToBusinessObject(dto));
          }
      }

      //[Transaction] : cần thiết lập một Transaction
      public void UpdatePermissonPerUsers(IList<PermissionsPerUsersDto> permissionsPerUserDtos, string usersID)
      {
          ////Delete all PermissionPerUsers of existing User
          this.PermissionsPerUsersDao.DeleteAllPermissionPerUserByUserId(usersID);

          //update new setting
          foreach (var dto in permissionsPerUserDtos)
          {
              this.PermissionsPerUsersDao.AddNewPermissionsPerUsers(this.PermissionsPerUsersAssembler.ConvertToBusinessObject(dto));
          }
      }



    }
}
